package simpleftp.server;

import java.net.ServerSocket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Simple serveur TCP creant des thread pour gerer chaque connexion client.
 * 
 * @author lom
 */
public class FTPServerMain {

	/**
	 * Port d'ecoute.
	 */
	public static final int PORT = 7802;
	/**
	 * Socket serveur d'accueil.
	 */
	private static ServerSocket socketaccueil;
	//for multithreading
	static ExecutorService threadExecutor;

	public static void main(String argv[]) throws Exception 
	{

		//TODO MAKE IT MULTITHREAD (about 4 lines) - DONE
		threadExecutor = Executors.newCachedThreadPool(); //executes as many threads as needed
		
		try {
			socketaccueil = new ServerSocket(PORT);
			System.out.println("Server up and running.");
			FTPServer.setDebug(true);
			while (true) {
				FTPServer server = new FTPServer(socketaccueil.accept());
				// TO BE COMPLETED - 1 LINE
				//TODO Modify for multithreading - DONE
				//server.call();
				threadExecutor.submit(server); //submit the callable server to the thread pool
			}
		} finally {
			if (socketaccueil != null) {
				socketaccueil.close();
				threadExecutor.shutdown(); //terminate all the threads of the pool
			}
		}
	}
}
